package com.chap1.waitandnotify;

/**
 * Created by IntelliJ IDEA.
 * User: bj910
 * Date:2023-03-28
 * Time: 5:13
 * To change this template use File | Settings | File Templates.
 */
public class Express {
    public final static String DIST_CITY = "ShangHai";
    public final static int TOTAL = 500;
    private int km;
    private String site;

    public Express() {
    }

    public Express(int km, String site) {
        this.km = km;
        this.site = site;
    }

    public void change(){
        if (km < TOTAL){
            km = km + 100;
            System.out.println("the Km is" + this.km);
        }
        if(km >= TOTAL){
            site = DIST_CITY;
            System.out.println("the Express is arrived");
        }
    }

    public synchronized void waitKm(){
        while(this.km<=TOTAL){
            try{
                wait();
                System.out.println("Map thread["
                        +Thread.currentThread().getId()
                        +"] wake,I will change db");
            }catch (Exception e){

            }
        }
    }

    public synchronized void waitSite(){
        while (! this.site.equals(DIST_CITY)){
            try{
                wait();
                System.out.println("Notice User thread["+Thread.currentThread().getId()
                        +"] wake");
            }catch (Exception e){

            }
        }
        System.out.println("the site is "+this.site+",I will call user");
    }
}
